package com.unistec.fileserver.server;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.unistec.fileserver.server.enums.FileType;
import com.unistec.fileserver.server.provider.IResourceSearchProvider;
import com.unistec.fileserver.server.provider.IStorageProvider;
import com.unistec.fileserver.server.provider.IUploadChainProvider;
import com.unistec.framework.utils.Objects;

/**
 * 上传服务工厂模式 （获取上传组件）
 * 
 * @author 韩凯
 * @date 2020年6月9日 下午 04:10
 */
@Component
public class UploadFactory {
	private static Logger logger = LoggerFactory.getLogger(UploadFactory.class);

	public static final String[] picTypes = new String[] { "jpg", "jpeg", "bmp", "gif", "tif", "png", "psd", "raw", "pneg" };
	public static final String[] videoTypes = new String[] { "avi", "mov", "rmvb", "rm", "mpg", "mpeg", "mp4", "3gp", "flv", "mp3", "wmv", "ram", "swf" };
	public static final String[] textTypes = new String[] { "txt", "text", "doc", "docx", "xls", "xlsx", "pdf", "ppt", "pptx", "inf" };

	private IUploadChainProvider uploadChainProvider;
	private IStorageProvider storageProvider;
	private IResourceSearchProvider resourceSearchProvider;

	@Autowired
	private Environment environment;

	public IUploadChainProvider getUploadChainProvider() {
		if (Objects.nonNull(uploadChainProvider)) {
			return uploadChainProvider;
		}
		String uploadChainProviderName = environment.getProperty("upload.provider.chainProvider");
		try {
			if (Objects.nonNull(uploadChainProviderName)) {
				// 尝试初始化SMS提供者
				Class<?> clazz = Class.forName(uploadChainProviderName);
				uploadChainProvider = (IUploadChainProvider) clazz.newInstance();
			} else {
				logger.error("使用了上传服务，但配置文件中未配置存储链条服务[upload.provider.chainProvider = null]");
				System.exit(-1);
			}
		} catch (InstantiationException ee) {
			logger.error("Cannot new IUploadChainProvider, because the provider doesnot have a constructor witout any method:" + uploadChainProviderName, ee);
			System.exit(-1);
		} catch (IllegalAccessException ie) {
			logger.error("Cannot new IUploadChainProvider, because the provider doesnot have a constructor witout any method:" + uploadChainProviderName, ie);
			System.exit(-1);
		} catch (ClassNotFoundException e) {
			logger.error("Cannot init IUploadChainProvider service module, because the provider class cannot find :" + uploadChainProviderName, e);
			System.exit(-1);
		} finally {
		}
		return uploadChainProvider;
	}

	/**
	 * 获取存储服务提供者
	 * 
	 * @return
	 */
	public IStorageProvider getStoragetProvider() {
		if (Objects.nonNull(storageProvider)) {
			return storageProvider;
		}
		String storageProviderName = environment.getProperty("upload.provider.storageProvider");
		try {
			if (Objects.nonNull(storageProviderName)) {
				// 尝试初始化SMS提供者
				Class<?> clazz = Class.forName(storageProviderName);
				storageProvider = (IStorageProvider) clazz.newInstance();
			} else {
				logger.error("使用了存储服务，但配置文件中未配置存储服务[upload.provider.storageProvider = null]");
				System.exit(-1);
			}
		} catch (InstantiationException ee) {
			logger.error("Cannot new IStorageProvider, because the provider doesnot have a constructor witout any method:" + storageProviderName, ee);
			System.exit(-1);
		} catch (IllegalAccessException ie) {
			logger.error("Cannot new IStorageProvider, because the provider doesnot have a constructor witout any method:" + storageProviderName, ie);
			System.exit(-1);
		} catch (ClassNotFoundException e) {
			logger.error("Cannot init IStorageProvider service module, because the provider class cannot find :" + storageProviderName, e);
			System.exit(-1);
		} finally {
		}
		return storageProvider;
	}

	public IResourceSearchProvider getResourceSearchProvider() {
		if (Objects.nonNull(resourceSearchProvider)) {
			return resourceSearchProvider;
		}
		String resourceSearchProviderName = environment.getProperty("upload.provider.resourceSearchProvider");
		try {
			if (Objects.nonNull(resourceSearchProviderName)) {
				// 尝试初始化SMS提供者
				Class<?> clazz = Class.forName(resourceSearchProviderName);
				resourceSearchProvider = (IResourceSearchProvider) clazz.newInstance();
			} else {
				logger.error("使用了存储服务，但配置文件中未配置存储服务[upload.provider.storageProvider = null]");
				System.exit(-1);
			}
		} catch (InstantiationException ee) {
			logger.error("Cannot new ITranscodeProvider, because the provider doesnot have a constructor witout any method:" + resourceSearchProviderName, ee);
			System.exit(-1);
		} catch (IllegalAccessException ie) {
			logger.error("Cannot new ITranscodeProvider, because the provider doesnot have a constructor witout any method:" + resourceSearchProviderName, ie);
			System.exit(-1);
		} catch (ClassNotFoundException e) {
			logger.error("Cannot init ITranscodeProvider service module, because the provider class cannot find :" + resourceSearchProviderName, e);
			System.exit(-1);
		} finally {
		}
		return resourceSearchProvider;
	}

	/**
	 * 获取上传文件的类型
	 * 
	 * @author 韩凯
	 * @date 2020年6月11日 下午 04:55
	 * @param fileName
	 */
	public static FileType getUploadFileType(String fileName) {
		int index = fileName.lastIndexOf(".");
		// 默认文本类型
		FileType fileType = FileType.OTHERS;
		if (index != -1 && fileName.length() != index + 1) {
			// 后缀名
			String suffixFileName = fileName.substring(index + 1).toLowerCase();
			List<String> list = Arrays.asList(videoTypes);
			if (list.contains(suffixFileName)) {
				fileType = FileType.VIDEO;
			} else if (Arrays.asList(picTypes).contains(suffixFileName)) {
				fileType = FileType.PIC;
			} else if (Arrays.asList(textTypes).contains(suffixFileName)) {
				fileType = FileType.DOC;
			} else {
				fileType = FileType.OTHERS;
			}
		}
		return fileType;
	}
}
